# 引入的内置模块
from math import e
import sys
import os
# 引入的第三方库
import pygame as pg
# 引入自己的模块
from main_tool import Virtual_board,pos_judge # 虚拟棋盘类和位置判断函数
import window_init
from window_init import icon_draw_factory # 水印动画函数
from window_init import draw_chessboard # 画棋盘函数
from player_sprite import Player # 玩家类
from config import * # 配置文件
from text import Text_BW  # 文字类
from text import Text

# 主程序
def windows_star(win,icon_png):
    # -------------------------------------变量参数
    M_my_x = 0
    M_my_y = 0
    # 跟随矩形的大小
    rect_size = 30
    # 玩家
    player_round = 1
    pointer = 0
    player_Bcolor = "黑"
    player_Wcolor = "白"
    # 输赢信号
    sign_win = False
    # ----------------------------------------

    # ---------------------------------------预加载
    # 虚拟棋盘
    list_pos_player = Virtual_board()
    # 精灵组
    group_player = pg.sprite.Group()
    # 字体初始化
    fond_B_img = Text(player_Bcolor, 100, (0, 0, 0,200))
    fond_W_img = Text(player_Wcolor, 100, (255, 255, 255,200))
    fond_B_win = Text_BW(f"{player_Bcolor}棋胜", 100, (0, 0, 0), (255, 255, 255), 3)
    fond_W_win = Text_BW(f"{player_Wcolor}棋胜", 100, (255, 255, 255), (0, 0, 0), 2)
    fond_tishi_B = Text_BW("按任意键重新开始，Esc退出游戏", 30, (0, 0, 0), (255, 255, 255), 2)
    fond_tishi_W = Text_BW("按任意键重新开始，Esc退出游戏", 30, (255, 255, 255), (0, 0, 0), 2)
    # 跟随图标
    image_cl_remove = pg.Surface((100,100), pg.SRCALPHA)
    # icon_draw函数的实例
    icon_draw = icon_draw_factory(icon_png)




    while True:
        # 背景
        win.fill((200, 200, 180))
        # 棋盘
        draw_chessboard(win)
        # 帧率
        pg.time.Clock().tick(60)
        # 水印动画
        icon_draw(win)
        # 双方轮次
        if player_round == 1:
            # win.blit(fond_B_img, (20, 250))
            fond_B_img.draw(win, (20, 250))
        else:
            # win.blit(fond_W_img, (680, 250))
            fond_W_img.draw(win, (680, 250))
        # 棋子
        group_player.draw(win)
        # 跟随图标
        pg.draw.circle(image_cl_remove,(pointer,pointer,pointer,150),(50,
                                   50),rect_size)
        win.blit(image_cl_remove, (M_my_x - 50, M_my_y - 50))

        # 输赢判断
        if sign_win:
            if player_round == 2:
                fond_B_win.draw(win, (250, 200))
                fond_tishi_B.draw(win, (180, 330))
            else:
                fond_W_win.draw(win, (250, 200))
                fond_tishi_W.draw(win, (180, 330))
            for event in pg.event.get():

                if event.type == pg.QUIT:
                    pg.quit()
                    sys.exit()
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_ESCAPE: # 退出游戏
                        pg.quit()
                        sys.exit()
                    else: # 重置游戏
                        list_pos_player.reset_board()
                        group_player.empty()
                        sign_win = False
                        player_round = 1
                        pointer = 0
                        image_cl_remove.fill((0, 0, 0, 0))
                        break
            pg.display.update()
            continue
            
            
        # ---------------------------------------------------------------------事件主循环
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: # 鼠标点击
                pos_judge(event.pos) # 计算最近的格子位置
                c_my_x,c_my_y = pos_judge(event.pos)

                # -------------------------------下棋
                if 15>c_my_x>-1 and 15>c_my_y>-1 and list_pos_player.board[c_my_x][c_my_y] == -1:
                    player : pg.sprite.Sprite = Player(CHESS_POS[c_my_x][c_my_y][0],
                                                      CHESS_POS[c_my_x][c_my_y][1],
                                                      player_round)

                    # noinspection PyTypeChecker
                    # 上一条注释用于忽略检查，不要动
                    group_player.add(player)
                    list_pos_player.board[c_my_x][c_my_y] = player_round
                    sign_win = list_pos_player.win_judge((c_my_x, c_my_y),player_round)

                    if player_round == 1:
                        player_round = 2
                        pointer = 255
                    else:
                        player_round = 1
                        pointer = 0
                # --------------------------------

            if event.type == pg.MOUSEMOTION: #鼠标移动
                M_my_x, M_my_y = event.pos

            if event.type == pg.MOUSEWHEEL: # 鼠标滚轮滑动
                if event.y == 1 and rect_size < 50:
                    rect_size += 5
                elif event.y == -1 and rect_size > 1:
                    rect_size -= 5
                image_cl_remove.fill((0, 0, 0, 0))
        # ---------------------------------------------------------------------

        pg.display.update()


def main():
    pg.init()
    # 函数和类的实例化
    icon_png = window_init.png()
    win = window_init.window_set(icon_png)
    
    pg.key.stop_text_input() # 禁用输入法
    # 主循环
    windows_star(win,icon_png)
    pg.quit()


if __name__ == '__main__':
    main()

